/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TagLibrary;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class InsertTag extends TagSupport{
    private String template;

    public void setTemplate(String template) {
        this.template = template;
    }
    public Stack getStack(){
        Stack stack=(Stack) this.pageContext.getAttribute(PageParameter.AttributeUtil.templeStack, PageContext.REQUEST_SCOPE);
        if(stack ==null){
            stack =new Stack();
            this.pageContext.setAttribute( PageParameter.AttributeUtil.templeStack, stack, PageContext.REQUEST_SCOPE);
        }
        return stack;
    }

    @Override
    public int doStartTag() throws JspException {
        Stack stack =this.getStack();
        Hashtable params=new Hashtable();
        stack.add(params);
        return EVAL_BODY_INCLUDE;
    }

   @Override
    public int doEndTag() throws JspException {
        try {
            this.pageContext.include(template);
            this.getStack().pop();
        } catch (ServletException ex) {
            Logger.getLogger(InsertTag.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InsertTag.class.getName()).log(Level.SEVERE, null, ex);
        }
        return EVAL_PAGE;
    }
}
